package com.seal.commons.validator.jsr303;

import java.text.ParseException;
import java.util.Date;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

public class DateTimeValidator implements ConstraintValidator<DateTime, String> {

	String[] patterns;
	
	@Override
	public void initialize(DateTime dateTime) {
		patterns = dateTime.parttern();
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
	    if(StringUtils.isBlank(value)){
            return true;
        }
		
		try{
			Date date = DateUtils.parseDate(value, patterns);
			return !(date == null);
		}
		catch(ParseException e){
			return false;
		}
	}
}
